<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Novel extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('novel_model');
        $this->load->model('category_model');
        $this->load->library('upload');
    }

    public function index() {
        $data = $this->novel_model->getNovel_all();
        $this->load->view('novel/all', array('novel' => $data));
    }
    
    public function add() {
        $cats = $this->category_model->getCategory_all();
        $this->load->view('novel/add', array('cats' => $cats));
    }

    public function edit($mid = 0) {
        if ($mid == 0) {
            show_404();
            die();
        }
        $novel = $this->novel_model->getNovel_by_id($mid);
        if ($novel) {
            $cats = $this->category_model->getCategory_all();
            $this->load->view('novel/edit', array('novel' => $novel, 'cats' => $cats));
        } else
            show_404();
    }
    
    public function ajax_add_new_novel() {
        if (!check_action_role(__FUNCTION__)) {
            echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
            die();
        }

        $arr = $this->novel_model->addNovel_validate($_POST);
        if ($arr['err'] == 0) {
            extract($_POST);
            $status = !isset($staus) ? 1 : $status;
            $data = array(
                'name' => trim($name),
                'description' => trim($desc),
                'author' => trim($author),
                'translator' => trim($translator),
                'oname' => trim($oname),
                'cover' => trim($cover),
                'status' => $status,
                'cats' => explode(',', $cats)
            );
            $arr['id'] = $this->novel_model->addNovel($data);
            $arr['err'] = $arr['id'] ? 0 : -1;
        }
        echo json_encode($arr);
    }
    
    public function ajax_upload_novel_cover() {
        if (!check_action_role(__FUNCTION__)) {
            echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
            die();
        }
        $result = $this->novel_model->uploadCover();

        $data = array();
        $data['err'] = $result['err'];
        $data['msg'] = $result['msg'];
        $relative = str_replace($_SERVER['DOCUMENT_ROOT'] . '/', "", $result['data']['full_path']);
        $data['furl'] = base_url() . $relative;
        $data['cover_path'] = $relative;

        echo json_encode($data);
    }
    
    public function ajax_edit_novel() {
        if (!check_action_role(__FUNCTION__)) {
            echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
            die();
        }
        $arr = $this->novel_model->addNovel_validate($_POST);
        if ($arr['err'] == 0) {
            extract($_POST);
            $role = !isset($role) ? 1 : $role;

            $data = array(
                'id' => $id,
                'name' => trim($name),
                'slug' => slug($name),
                'description' => trim($desc),
                'author' => trim($author),
                'translator' => trim($translator),
                'oname' => trim($oname),
                'cover' => trim($cover),
                'status' => $status,
                'cats' => explode(',', $cats)
            );
            $arr['err'] = $this->novel_model->editNovel($data) ? 0 : -1;
            $arr['slug'] = slug($name);
        }
        echo json_encode($arr);
    }
    
    function ajax_remove_novel() {
        if (!check_action_role(__FUNCTION__)) {
            echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
            die();
        }

        if (!isset($_POST['id'])) {
            echo json_encode(array('err' => -1, 'msg' => 'Missing parameter.'));
            die;
        }

        $id = $_POST['id'];
        $rm = $this->novel_model->removeNovel($id);
        if ($rm)
            echo json_encode(array('err' => 0, 'msg' => 'Successfull.'));
        else
            echo json_encode(array('err' => -1, 'msg' => 'Something wrong...'));
    }
    
    /**
     * Controller to create new chapter
     */
    function ajax_add_new_chapter() {
        if (!check_action_role(__FUNCTION__)) {
            echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
            die();
        }

        if (!isset($_POST['novelId']) || !isset($_POST['chapterId'])) {
            echo json_encode(array('err' => -1, 'msg' => 'Missing parameter.'));
            die;
        }

        extract($_POST);
        $arr = array();
        if (!is_numeric($novelId) || !is_numeric($chapterId)) {
            $arr['err'] = -1;
            $arr['msg'] = 'Invalid parameter.';
        } else if (count($this->novel_model->getChapter_by_chapterId($novelId, $chapterId)) > 0) {
            $arr['err'] = -1;
            $arr['msg'] = 'Chapter exist.';
        } else {
            $arr['err'] = $this->novel_model->addNewChapter($novelId, $chapterId) ? 0 : -1;
        }
        echo json_encode($arr);
    }
    
    /**
     * Ajax to get images of a chapter
     */
    function ajax_get_chapter_content() {
        if (!check_action_role(__FUNCTION__)) {
            echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
            die();
        }
        $arr = $this->novel_model->getChapter_validate($_POST);
        if ($arr['err'] == 0) {
            extract($_POST);
            $data = $this->novel_model->getChapter_by_chapterId($novelId, $chapterId);
            if(count($data) > 0)
                $arr['data'] = $data[0];
        }
        echo json_encode($arr);
    }
    
    /**
     * Ajax to delete a chapter
     */
    function ajax_delete_chapter() {
        if (!check_action_role(__FUNCTION__)) {
            echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
            die();
        }

        if (!isset($_POST['novelId']) || !isset($_POST['chapterId'])) {
            echo json_encode(array('err' => -1, 'msg' => 'Missing Parameter'));
            die();
        }
        extract($_POST);
        $slug = !isset($_POST['slug']) ? $this->novel_model->getManga_slug_by_id($novelId) : $_POST['slug'];
        $this->novel_model->deleteChapter_by_id($slug, $chapterId, $novelId);
        echo json_encode(array('err' => 0, 'msg' => ''));
    }
    
    /**
     * Ajax to save images to a chapter
     * This will serialize the images list and update to chapter
     */
    function ajax_save_chapter_content() {
        if (!check_action_role(__FUNCTION__)) {
            echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
            die();
        }

        extract($_POST);
        $content = isset($content) ? $content : '';
        $data = array('novelId' => $novelId, 'chapterId' => $chapterId, 'status' => $status, 'content' => $content, 'title' => $title);
        $arr['err'] = $this->novel_model->updateChapter_by_chapterId($data) ? 0 : -1;
        echo json_encode($arr);
    }
}
